%RR tf
% importance - the simple matrix notations l
% uses the matrix notations
% latest date 4.5.3

function[tf]=tf_r(SPMatrix),


if isstruct(SPMatrix) == 1, % for structur arryay
   nfreq = length(SPMatrix.spectra); % in momentory lapse of common sense i adopted structure
      for i = 1:nfreq,
   	SPM(i,:,:) = SPMatrix.spectra(i).data;
   end;
   clear SPMatrix;
   SPMatrix=SPM;
end;

A = size(SPMatrix);


if length(A) == 2,
   data(:,:)=SPMatrix;
   [ER,HR]=getmat(data);
   Z = ER/HR; % i
   tf(1)=Z(1,2);% boring !! but i have to do this
   tf(2)=Z(2,1);
   tf(3)=Z(1,1);
   tf(4)=Z(2,2);
elseif length(A)==3,
   for i =1:A(1),
      data(:,:)=SPMatrix(i,:,:);
     [ER,HR]=getmat(data);
   Z = ER/HR; % 
   tf(i,1)=Z(1,2);% boring !! but i have to do this
   tf(i,2)=Z(2,1);
   tf(i,3)=Z(1,1);
   tf(i,4)=Z(2,2);
end;
 
end;
tf(:,5:8)=0; %variance need to be added 20.4.3
   
   
   
%helper function


function[ER,HR]=getmat(data),

j = sqrt(-1);


HxHxr = data(6,1)-j*data(1,6);
HxrHx = conj(HxHxr);
HxHyr = data(7,1)-j*data(1,7);
HyrHx = conj(HxHyr);

HyHxr = data(6,2)-j*data(2,6);
HxrHy = conj(HyHxr);
HyHyr = data(7,2)-j*data(2,7);
HyrHy = conj(HyHyr);

ExHxr = data(6,4)-j*data(4,6);
HxrEx = conj(ExHxr);
EyHxr = data(6,5)-j*data(5,6);
HxrEy = conj(EyHxr);

ExHyr = data(7,4)-j*data(4,7);
HyrEx = conj(ExHyr);
EyHyr = data(7,5)-j*data(5,7);
HyrEy = conj(EyHyr);

ER=[ExHxr ExHyr;EyHxr EyHyr];
HR=[HxHxr HxHyr;HyHxr HyHyr];

